package thread;

public class Demo27 {
    public static Object locker = new Object();
    public static Object locker1 = new Object();
    public static Object locker2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                synchronized (locker2) {
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                synchronized (locker) {
                    System.out.print('A');
                    locker.notify();
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                synchronized (locker) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print('B');
                synchronized (locker1) {
                    locker1.notify();
                }
            }
        });
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                synchronized (locker1) {
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println('C');
                }
                synchronized (locker2) {
                    locker2.notify();
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(100);

        synchronized (locker2) {
            locker2.notify();
        }
    }
}
